package com.css.fxfzfxqh.modules.ysx.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.ysx.entity.FxqhJzwYSXSub;
import com.css.fxfzfxqh.modules.ysx.param.FxqhJzwYSXParam;
import com.css.fxfzfxqh.modules.ysx.param.MatchingParam;
import com.css.fxfzfxqh.modules.ysx.service.JzwYSXService;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.ResponseCreator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/08/22
 * @Description: 易损性正式便
 */
@Slf4j
@RestController
@RequestMapping("/jzwysx")
public class JzwYSXController {
    @Resource
    private JzwYSXService jzwYSXService;

    /**
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, FxqhJzwYSXParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = jzwYSXService.getPage(curPage, pageSize, param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @PostMapping("/uploadYsxExcel")
    public RestResponse uploadRcpInformation(@RequestParam MultipartFile file,String jsxType,String province,String city,String county,String roleType) {
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        if (StringUtils.isBlank(jsxType)) {
            return RestResponse.fail("未传入对应类型!");
        }else if (!"1".equals(jsxType) && !"2".equals(jsxType)) {
            return RestResponse.fail("传入对应类型不符!");
        }
        RestResponse response = null;
        try{
            Map<String, Object> map =jzwYSXService.uploadYsxInformation(file,jsxType,province,city,county,roleType);
            boolean res = ((Boolean) map.get("res")).booleanValue();
            if(res){
                response = RestResponse.succeed("上传成功!");
                response.setMessage("上传成功!");
            }else {
                response = RestResponse.fail("上传失败!");
                response.setMessage(map.get("mess").toString());
            }
        }catch (Exception e){
            String errormessage = "上传失败!";
            response=RestResponse.fail(errormessage);
            e.printStackTrace();
        }
        return response;
    }

    /**
     * 获取矩阵列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPageById")
    public RestResponse getPageById(HttpServletRequest request, FxqhJzwYSXParam param) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            Map<String, Object> page = jzwYSXService.getPageById( param);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 自定义易损性生成
     */
    @PostMapping("/saveOrUpdate")
    public  RestResponse saveOrUpdate(@RequestBody MatchingParam param){
        try {
            String id = jzwYSXService.saveOrUpdate(param);
            if("-1".equals(id)){
                return RestResponse.fail("对应国家级易损性数据缺失或不完整，生成失败!");
            }
            return RestResponse.succeed(id);
        } catch (Exception e) {
            log.error("post save error:{}",e.getMessage(),e);
            return RestResponse.fail("生成失败!");
        }
    }

    /**
     * 自定义易损性生成
     */
    @GetMapping("/getSfIntensity")
    public  RestResponse getSfIntensity(FxqhJzwYSXParam param){
        try {
            List<String> list = jzwYSXService.getSfIntensity(param);
            return RestResponse.succeed(list);
        } catch (Exception e) {
            log.error("post save error:{}",e.getMessage(),e);
            return RestResponse.fail("生成失败!");
        }
    }

    @PostMapping("/update")
    public  RestResponse getSfIntensity(@RequestBody  List<FxqhJzwYSXSub> param) throws  Exception{
        jzwYSXService.update(param);
        return ResponseCreator.createSuccessResponse(null,"修改成功");
    }

}
